/*
 * CampaignEditorWindow.cpp
 *
 *  Created on: 12/06/2014
 *      Author: javier
 */

#include "CampaignEditorWindow.h"
#include "LevelEditorWindow.h"
#include  <iostream>
#include "Utils.h"
#include <map>

CampaignEditorWindow::CampaignEditorWindow(BaseObjectType* cobject,
		const Glib::RefPtr<Gtk::Builder>& refGlade) :
		Gtk::Window(cobject) {
	set_default_size(properties.getPropertyAsInt("editor.window.width"),
			properties.getPropertyAsInt("editor.window.height"));
	set_title(properties.getProperty("editor.name").c_str());

	campania = NULL;

	refGlade->get_widget("btnNewScenario", btnNewScenario);
	refGlade->get_widget("btnSave", btnSave);
	refGlade->get_widget("btnEdit", btnEdit);

	btnNewScenario->signal_clicked().connect(
			sigc::mem_fun(*this, &CampaignEditorWindow::newScenario));
	btnSave->signal_clicked().connect(
			sigc::mem_fun(*this, &CampaignEditorWindow::save));
	btnEdit->signal_clicked().connect(
			sigc::mem_fun(*this, &CampaignEditorWindow::editScenario));

	refGlade->get_widget("inputIDCampaign", inputIDCampaign);
	refGlade->get_widget("inputCantPlayers", inputCantPlayers);
	refGlade->get_widget("inputDificult", inputDificultad);
	refGlade->get_widget("inputName", inputName);
	refGlade->get_widget("inputImage", inputImage);

	refGlade->get_widget("comboLevels", comboLevels);

	refGlade->get_widget("inputNewX", inputNewLevelX);
	refGlade->get_widget("inputNewY", inputNewLevelY);

	newLevelX = newLevelY = 5;
	levelEditor = NULL;
}

CampaignEditorWindow::~CampaignEditorWindow() {
	// TODO Auto-generated destructor stub
}

void CampaignEditorWindow::setCampania(Campania* c) {
	this->campania = c;
}

void CampaignEditorWindow::save() {
	Glib::ustring idCamp = inputIDCampaign->get_text();
	std::cout << "Nuevo id de campania: " << idCamp << std::endl;
	campania->setId(Utils::stringToInt(idCamp));

	Glib::ustring jugadores = inputCantPlayers->get_text();
	std::cout << "cant de jugadores: " << jugadores << std::endl;
	campania->setCantJugadoresMin(Utils::stringToInt(jugadores));

	Glib::ustring dif = inputDificultad->get_text();
	std::cout << "dificultad: " << dif << std::endl;
	campania->setNivelDificultad(Utils::stringToInt(dif));

	Glib::ustring nombre = inputName->get_text();
	std::cout << "nombre: " << nombre << std::endl;
	campania->setNombre(nombre);

	Glib::ustring image = inputImage->get_text();
	std::cout << "imagen: " << image << std::endl;
	campania->setImagen(image);

	controller->save();
}

void CampaignEditorWindow::newScenario() {
	newLevelX = Utils::stringToInt(inputNewLevelX->get_text());
	newLevelY = Utils::stringToInt(inputNewLevelY->get_text());
	Nivel* n = controller->newScenario();
	n->setMapHeight(newLevelY);
	n->setMapWidth(newLevelX);
	std::string repr;
	for (int x = 0; x < newLevelX; ++x) {
		for (int y = 0; y < newLevelY; ++y) {
			repr += "0";
		}
		repr += "\n";
	}
	n->setTileMapRepresentation(repr);
	levelEditor->setLevel(n);
	levelEditor->show_all();
}

void CampaignEditorWindow::editScenario() {
	Glib::ustring id = comboLevels->get_entry_text();
	int iID = Utils::stringToInt(id);
	std::map<int, Nivel*> &levels = campania->getSecuenciaNiveles();
	std::map<int, Nivel*>::iterator it;
	for (it = levels.begin(); it != levels.end(); ++it) {
		Nivel* n = it->second;
		if (n->getId() == iID) {
			levelEditor->setLevel(n);
			levelEditor->show_all();
			return;
		}
	}
	Gtk::MessageDialog dlg("El numero de ID de nivel no existe", false,
			Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true);
	dlg.set_title("ID de nivel incorrecto");
	dlg.run();
//	levelEditor->setLevel(NULL);
//	levelEditor->show_all();
}

void CampaignEditorWindow::loadCampaignInfo() {
	if (campania) {
		inputIDCampaign->set_text(Utils::intToString(campania->getId()));
		inputCantPlayers->set_text(
				Utils::intToString(campania->getCantJugadoresMin()));
		inputDificultad->set_text(
				Utils::intToString(campania->getNivelDificultad()));
		inputName->set_text(campania->getNombre());
		inputImage->set_text(campania->getImagen());
	}
}

const LevelEditorWindow* CampaignEditorWindow::getLevelEditor() const {
	return levelEditor;
}

void CampaignEditorWindow::setLevelEditor(LevelEditorWindow* levelEditor) {
	this->levelEditor = levelEditor;
}

void CampaignEditorWindow::setController(EditorController* controller) {
	this->controller = controller;
}
